# Maintainer: Thomas Liske <thomas@fiasko-nw.net>
pkgname=pypy3
pkgver=7.3.12
_pkgver=${pkgver/_/}
_pyver=3.10
pkgrel=0
pkgdesc="Fast, compliant alternative implementation of Python $_pyver"
url="https://www.pypy.org/"
arch="x86 x86_64 aarch64 s390x"
license="MIT"
makedepends="
	bzip2-dev
	expat-dev
	gdbm-dev
	libffi-dev
	linux-headers
	ncurses-dev
	openssl-dev>3
	pypy
	readline-dev
	sqlite-dev
	tk-dev
	util-linux-misc
	xz-dev
	zlib-dev
	"
subpackages="$pkgname-dev $pkgname-tests $pkgname-tkinter $pkgname-pyc"
source="https://downloads.python.org/pypy/pypy$_pyver-v$_pkgver-src.tar.bz2
	0001-time.patch
	0002-extern-stdio.patch
	"
builddir="$srcdir/pypy$_pyver-v$_pkgver-src"
options="!check" # todo

[ "$CARCH" = "x86" ] && options="$options textrels" # on x86 has textrels

build() {
	if [ "$JOBS" -gt 16 ]; then
		export JOBS=16
	fi

	msg "building pypy3"
	pypy rpython/bin/rpython --opt=jit --shared --make-jobs=${JOBS:-2} pypy/goal/targetpypystandalone.py

	msg "configuring system dirs"
	./pypy3.10-c -m sysconfig --generate-posix-vars \
		LIBDIR=/usr/lib \
		TZPATH=/usr/share/zoneinfo \
		INCLUDEPY=/usr/include/pypy$_pyver

	local out="$(cat pybuilddir.txt)"
	cp "$out"/_sysconfigdata__*.py lib-python/3/

	msg "generating pattern pickles"
	./pypy$_pyver-c - <<-EOF
		import lib2to3.pygram
		import lib2to3.patcomp
		lib2to3.patcomp.PatternCompiler()
	EOF

	msg "compiling binary modules"
	./pypy$_pyver-c lib_pypy/pypy_tools/build_cffi_imports.py

	# cleanup pypy libdir
	find lib_pypy -name "*_cffi.[co]" -delete
	find lib_pypy -type d -empty -delete
}

check() {
	./pypy$_pyver-c -u pypy/test_all.py \
		pypy/module/pypyjit/test_pypy_c
}

package() {
	mkdir -p "$pkgdir"/usr/lib/pypy$_pyver "$pkgdir"/usr/include

	# install stdlib
	# this puts everything just like python itself does
	cp -a lib-python/3/. lib_pypy/. "$pkgdir"/usr/lib/pypy$_pyver/

	# install includes
	cp -a include/pypy$_pyver "$pkgdir"/usr/include/

	# install pypy itself
	install -Dm755 ./pypy$_pyver-c -t "$pkgdir"/usr/bin
	install -Dm755 ./libpypy$_pyver-c.so -t "$pkgdir"/usr/lib/

	# install symlink
	ln -sfv pypy$_pyver-c "$pkgdir"/usr/bin/pypy3

	# allow fail for 2to3 module
	./pypy$_pyver-c -m compileall -fq "$pkgdir"/usr/lib || true
}

dev() {
	default_dev

	# pyconfig.h is needed at runtime
	mkdir -p "$pkgdir"/usr/include/pypy$_pyver
	mv "$subpkgdir"/usr/include/pypy$_pyver/pyconfig.h \
		"$pkgdir"/usr/include/pypy$_pyver/
}

tests() {
	pkgdesc="$pkgdesc (test module)"

	amove \
		usr/lib/pypy$_pyver/test \
		usr/lib/pypy$_pyver/*/test \
		usr/lib/pypy$_pyver/*/tests
}

pyc() {
	pkgdesc="Precompiled Python bytecode for ${subpkgname%-pyc}"
	install_if="${subpkgname%-pyc}=$pkgver-r$pkgrel pyc"

	cd "$pkgdir"
	local IFS='
'
	# shellcheck disable=2046
	amove $(find usr/lib/pypy$_pyver -type d -name __pycache__)
}

tkinter() {
	pkgdesc="$pkgdesc (tkinter module)"

	amove usr/lib/pypy$_pyver/_tkinter \
		usr/lib/pypy$_pyver/tkinter
}

sha512sums="
080520c7de267e17a2823f06cd0a15c108a2c32fc296d6e24782050eb546fa74852d948ede2c381dba110774121b20efc3a07b553acd0ac0db4a844dfc09069e  pypy3.10-v7.3.12-src.tar.bz2
42abc8c67c249ff012010aec568579cd738ed2d74ffdf93755c9427117e85cd859390266c7431fcb4d2fcfd82b61d5ba0c58bd0ceda44e5d12bf22df5935bb24  0001-time.patch
b648204914072e74367b94065d65b423324bcbabc0e2199ad195ab9822ccc6c51ec552c8c17ed22c26caf73b04d16d6f4c3e14a8d1af0e378d76d6c3b587b784  0002-extern-stdio.patch
"
